/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import administrators.DataUser;
import administrators.IT;
import java.util.HashMap;
import java.util.Map;
import administrators.User;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.properties.EncryptableProperties;

/**
 *
 * @author ayle
 */
public class MUsers {

    private static MUsers mUsers;
    private Map usersList;

    private MUsers() {
        usersList = new HashMap<String, User>();
    }

    public static MUsers getMUsers() {
        if (mUsers == null) {
            mUsers = new MUsers();
        }
        return mUsers;
    }

    public boolean existNick(String nick) {
        return usersList.containsKey(nick.toLowerCase());
    }

    public boolean existMail(String mail) {
        boolean success = false;
        Iterator it = usersList.values().iterator();
        while (!success && it.hasNext()) {
            User user = (User) it.next();
            success = user.getEmail().equalsIgnoreCase(mail);
        }
        return success;
    }

    public boolean existUser(String nick, String email) {
        Iterator it = usersList.values().iterator();
        while (it.hasNext()) {
            User user = (User) it.next();
            if (user.getNick().equalsIgnoreCase(nick) || (user.getEmail().equalsIgnoreCase(nick))) {
                return true;
            }
        }
        return false;
    }

    public void newUser(String nick, String email, String name, String surname, String pass) throws Exception {
        if (existNick(nick)) {
            throw new Exception("Error. Existing nick!");
        }
        if (existMail(email)) {
            throw new Exception("Error. Existing mail!");
        }
        try {
            encryptAndStorePass(nick, pass);
        } catch (Exception ex) {
            throw new Exception("Error saving pass!");
        }
        IT newuser = new IT(nick, name, surname, email);
        usersList.put(nick, newuser);
    }

    public void editUser(String nick, String newnick, String email, String name, String surname, String pass) throws Exception {
        if (!existNick(nick)) {
            throw new Exception("Error. " + nick + " is not a valid nick!");
        }
        User user = (User) usersList.get(nick);
        if (!user.getEmail().equals(email) && existMail(email)) {
            throw new Exception("Error. Existing user with mail" + email + "!");
        }
        try {
            deletePass(nick);
            encryptAndStorePass(nick, pass);
        } catch (Exception ex) {
            throw new Exception("Error saving pass!");
        }
        user.setEmail(email);
        user.setName(name);
        user.setSurname(surname);
        user.setNick(newnick);
        usersList.remove(nick);
        usersList.put(nick, newnick);
    }

    public List listUsers() {
        List lret = new ArrayList();
        Iterator it = usersList.values().iterator();
        while (it.hasNext()) {
            User user = ((User) it.next());
            lret.add(new DataUser(user.getNick(), user.getEmail(), user.getName(), user.getSurname()));
        }
        return lret;
    }

    private void encryptAndStorePass(String nick, String pass) throws FileNotFoundException, IOException {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("4$1M0v");
        String passEnc = encryptor.encrypt(pass);
        Properties props = new EncryptableProperties(encryptor);
        props.load(new FileInputStream("passwords.properties"));
        props.setProperty(nick.toLowerCase(), passEnc);
        props.store(new FileOutputStream("passwords.properties"), "");
    }

    public void deletePass(String nick) throws FileNotFoundException, IOException {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("4$1M0v");
        Properties props = new EncryptableProperties(encryptor);
        props.load(new FileInputStream("passwords.properties"));
        props.remove(nick);
        props.store(new FileOutputStream("passwords.properties"), "");
    }

    public boolean validatePass(String nick, String pass) throws FileNotFoundException, IOException {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("4$1M0v");
        Properties props = new EncryptableProperties(encryptor);
        props.load(new FileInputStream("passwords.properties"));
        String passEnc = props.getProperty(nick.toLowerCase());
        String passDec = encryptor.decrypt(passEnc);
        return passDec.equals(pass);
    }
}
